package uutalk.netcloud.talk.base.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;

import uutalk.netcloud.talk.base.util.thirdpart.TalkManager;

/**
 * 类描述：
 * 作者：Pengmutian
 * 时间：2018/6/1
 */
public class AudioSpeekButton extends AppCompatButton {

    private static final String TAG = "AudioSpeekButton";
    // 三个状态
    private static final int MSG_AUDIO_PREPARED = 0X110;
    private static final int MSG_VOICE_CHANGE = 0X111;
    private static final int MSG_DIALOG_DIMISS = 0X112;

    //录音对话框
    private DialogManager mDialogManager;
    //上下文
    Context mContext;
    //震动类
    private Vibrator vibrator;
    // 正在录音标记
    public boolean isSpeeking = false;

    public AudioSpeekButton(Context context) {
        this(context, null);
    }

    public AudioSpeekButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public AudioSpeekButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        //初始化语音对话框
        mDialogManager = new DialogManager(getContext());
    }

    @Override
    public boolean isInEditMode() {
        return true;
    }

    // 获取音量大小的runnable
    private Runnable mGetVoiceLevelRunnable = new Runnable() {

        @Override
        public void run() {
            while (isSpeeking) {
                try {
                    Thread.sleep(100);
                    mStateHandler.sendEmptyMessage(MSG_VOICE_CHANGE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    private Handler mStateHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_AUDIO_PREPARED:
                    // 显示应该是在audio end prepare之后回调
                    mDialogManager.showRecordingDialog();
                    new Thread(mGetVoiceLevelRunnable).start();
                    // 需要开启一个线程来变换音量
                    break;
                case MSG_VOICE_CHANGE:
                    mDialogManager.updateVoiceLevel(TalkManager.getInstance().getMicLevel());
                    break;
                case MSG_DIALOG_DIMISS:
                    mDialogManager.dimissDialog();
                    break;
            }
        }
    };

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "ACTION_DOWN");
                isSpeeking = true;
                mStateHandler.sendEmptyMessage(MSG_AUDIO_PREPARED);
                TalkManager.getInstance().openMic(true);
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "ACTION_UP");
                isSpeeking = false;
                mStateHandler.sendEmptyMessage(MSG_DIALOG_DIMISS);
                TalkManager.getInstance().openMic(false);
                mDialogManager.dimissDialog();
                break;
        }
        return true;
    }

    @Override
    public boolean onPreDraw() {
        return false;
    }

    public void onPressTalk(boolean isTalk){
        if(isTalk){
            Log.e(TAG, "onPressTalk(boolean isTalk True!");
            isSpeeking = true;
            mStateHandler.sendEmptyMessage(MSG_AUDIO_PREPARED);
            TalkManager.getInstance().openMic(true);
        } else {
            Log.e(TAG, "onPressTalk(boolean isTalk False!");
            isSpeeking = false;
            mStateHandler.sendEmptyMessage(MSG_DIALOG_DIMISS);
            TalkManager.getInstance().openMic(false);
            mDialogManager.dimissDialog();
        }
    }
}
